﻿Public Class DataWindow : Inherits BaseControl

	Dim Rs As Recordset
	Dim IntFields As New List(Of Field)
	Dim Labels As New SortedList(Of String, String)
	Dim Groups As New List(Of Group)

	Public SortedFields As Lookup(Of String, Field)

	Sub New()
		DoubleBuffered = True
		CreateLabels()
	End Sub

	Event FieldValidating(Control As TextBoxBase, e As System.ComponentModel.CancelEventArgs)

	Property FieldPadding As Integer = 16

	ReadOnly Property Fields(Key As String) As Field
		Get
			Return SortedFields(Key).First
		End Get
	End Property

	Property Columns As Integer = 3

	Property Rate As Double = 0.33

	Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, keyData As System.Windows.Forms.Keys) As Boolean
		If keyData = Keys.Down Then
			If Pressed Is Nothing Then Return True
			Dim i = IntFields.IndexOf(Pressed)
			If i = IntFields.Count - 1 Then Return True
			Pressed = IntFields(i + 1)
			Return True
		ElseIf keyData = Keys.Up Then
			If Pressed Is Nothing Then Return True
			Dim i = IntFields.IndexOf(Pressed)
			If i = 0 Then Return True
			Pressed = IntFields(i - 1)
			Return True
		End If
		Return MyBase.ProcessCmdKey(msg, keyData)
	End Function

	Protected Overrides Function IsInputKey(keyData As System.Windows.Forms.Keys) As Boolean
		If keyData = Keys.Down Then Return True
		If keyData = Keys.Up Then Return True
		Return MyBase.IsInputKey(keyData)
	End Function

	Shadows DesignMode As Boolean = False

	Property Recordset As Recordset
		Get
			Return Rs
		End Get
		Set(Value As Recordset)
			Rs = Value
			SuspendLayout()
			If DesignMode Then
				For Each Col As DataColumn In Rs.Columns
					If Not SortedFields.Contains(Col.ColumnName) Then AddField(Col.ColumnName)
				Next
			End If
			For Each Field In IntFields
				If Rs.Columns.Contains(Field.Name) AndAlso Not Rs.EOF Then
					If Not Field.Control Is Nothing Then
						Field.Control.Value = Rs(Field.Name)
						If Field.Name = "odeyerkod" Then
							Fields("odeyerkodbanka").Value = Muhasebe.OdemeYerleriBanka(Process.Remote, Rs(Field.Name))
							Fields("odeyerkodsube").Value = Rs(Field.Name)
						End If
					End If
				End If
			Next
			Rearrange()
			ResumeLayout()
			Invalidate()
		End Set
	End Property

	Dim _Object As Object
	Property [Object] As Object
		Get
			Return _Object
		End Get
		Set(Value As Object)
			_Object = Value
			SuspendLayout()
			If DesignMode Then
				For Each p In Value.GetType.GetProperties
					If Not SortedFields.Contains(p.Name) Then AddField(p.GetValue(Value, Nothing))
				Next
			End If
			For Each Field In IntFields
				If Not Value Is Nothing And Not Field.Control Is Nothing Then
					Field.Control.Value = CallByName(Value, Field.Name, CallType.Get)
				End If
			Next
			Rearrange()
			ResumeLayout()
		End Set
	End Property

	Dim _Selected As Field
	Property Selected As Field
		Get
			Return _Selected
		End Get
		Set(Value As Field)
			If Value Is Selected Then Return
			If Not Selected Is Nothing Then Selected.Invalidate()
			_Selected = Value
			If Not Selected Is Nothing Then Selected.Invalidate()
		End Set
	End Property

	Dim _Pressed As Field
	Property Pressed As Field
		Get
			Return _Pressed
		End Get
		Set(Value As Field)
			If Value Is Pressed Then Return
			If Not Pressed Is Nothing Then Pressed.Invalidate() : Pressed.ShowHide(False)
			_Pressed = Value
			If Not Pressed Is Nothing Then Pressed.Invalidate() : Pressed.ShowHide(True) : Pressed.Control.Focus()
		End Set
	End Property

	Dim _Focused As Field
	Shadows Property Focused As Field
		Get
			Return _Focused
		End Get
		Set(Value As Field)
			If Value Is Focused Then Return
			If Not Focused Is Nothing Then Focused.Invalidate()
			_Focused = Value
			If Not Focused Is Nothing Then Focused.Invalidate()
			If Not Focused Is Nothing Then Focused.Control.Focus()
		End Set
	End Property

	Public Overrides Sub Reset()
		MyBase.Reset()
		Controls.Clear()
		Groups.Clear()
		IntFields.Clear()
		Invalidate()
	End Sub

	Dim EditingControl As BaseControl

	Function AddField(Name As String) As Field
		Return AddField(Name, New Group)
	End Function

	Function AddField(Name As String, Group As Group) As Field
		Dim Locked As Boolean = False
		Dim Enabled As Boolean = True
		If Name.EndsWith("**") OrElse Name.StartsWith("**") Then
			Enabled = False
			Name = Name.Replace("*", "")
		ElseIf Name.EndsWith("*") OrElse Name.StartsWith("*") Then
			Locked = True
			Name = Name.Replace("*", "")
		End If
		Dim Type = Name
		If InStr(Name, ":") > 0 Then
			Dim d = Name.Split(":")
			Name = d(0)
			Type = d(1)
		End If
		Dim Field As New Field(Me, Name, Type)
		IntFields.Add(Field)
		Field.Locked = Locked
		Field.Enabled = Enabled
		Field.Display = If(Labels.ContainsKey(Name), Labels(Name), If(Labels.ContainsKey(Type), Labels(Type), Field.Type))
		AddHandler Field.Control.GenericEvent, AddressOf FieldGenericEvent
		AddHandler Field.Control.Leave, AddressOf FieldLeave
		AddHandler Field.Control.Validating, AddressOf FieldValidatingHandler
		SortedFields = IntFields.ToLookup(Function(p) p.Name)
		Field.Group = Group
		Group.Fields.Add(Field)
		If Type = "odeyerkod" Then
			AddField("odeyerkodbanka", Group)
			AddField("odeyerkodsube", Group)
		End If
		Return Field
	End Function

	Function AddField(Label As String, Name As String) As Field
		Labels.Add(Name.Replace("*", ""), Label)
		Return AddField(Name)
	End Function

	Function AddField(Label As String, Name As String, Type As String) As Field
		Labels.Add(Name.Replace("*", ""), Label)
		Dim Field = AddField(Name)
		Return Field
	End Function

	Sub AddLabel(Key As String, Label As String)
		Labels.Add(Key, Label)
	End Sub

	Sub Rearrange()
		Dim x As Integer = FieldPadding, y As Integer = FieldPadding
		Dim w As Integer = (ClientSize.Width - FieldPadding * (Columns + 1)) \ Columns
		Dim h As Integer = 24
		SuspendLayout()
		For Each Group In Groups
			If Group.Name <> "" Then
				If y + (Group.Fields.Count + 1) * h + FieldPadding > ClientSize.Height Then
					x += w + FieldPadding
					y = FieldPadding
				End If
				Group.Rect = New Rectangle(x, y, w, h)
				y += h
				For Each Field In Group.Fields
					Field.Locate(New Rectangle(x, y, w, h))
					y += h  '+ 2
				Next
				y += FieldPadding
			Else
				For Each Field In Group.Fields
					Controls.Remove(Field.Control)
				Next
			End If
		Next
		For Each Field In IntFields
			If Field.Group.Name = "" Then
				Field.Locate(New Rectangle(x, y, w, h))
				y += h '+ 2
				If y + h + FieldPadding > ClientSize.Height Then
					x += w + FieldPadding
					y = FieldPadding
				End If
			End If
		Next
		ResumeLayout()
		Invalidate()
	End Sub

	Protected Overrides Sub OnPaint(e As System.Windows.Forms.PaintEventArgs)
		MyBase.OnPaint(e)
		Dim g = e.Graphics
		g.TextRenderingHint = Drawing.Text.TextRenderingHint.ClearTypeGridFit
		'e.Graphics.FillRectangle(Themes.Current.FormColor.Mix(Color.White, 20).Brush, ClientRectangle)
		For Each Group In Groups
			Dim R = Group.Rect
			g.SmoothingMode = Drawing2D.SmoothingMode.None
			g.PixelOffsetMode = Drawing2D.PixelOffsetMode.None
			Dim Pen As New Pen(Themes.Current.FormColor)
			Pen.DashStyle = Drawing2D.DashStyle.Dot
			'g.DrawRectangle(Pen, R)
			Dim r1 = Group.Rect
			Dim r2 = Group.Fields.Last.Rect
			R = New Rectangle(r1.Left, r1.Top, r2.Right - r1.Left + 1, r2.Bottom - r1.Top + 1)
			g.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
			g.PixelOffsetMode = Drawing2D.PixelOffsetMode.HighQuality
			'g.FillRoundRect(R, Themes.Current.FormColor.Opacity(If(Group.Fields.Contains(Pressed), 128, 64)).Brush, 4)
			'g.PixelOffsetMode = Drawing2D.PixelOffsetMode.None
			R.Width -= 1 : R.Height -= 1
			'g.DrawRoundRect(R, Color.Black.Opacity(32).Pen, 4)
			g.FillRoundRect(r1, Themes.Current.FormColor.Opacity(If(Group.Fields.Contains(Pressed), 255, 128)).Brush, 4)
			r1.Inflate(-4, 0)
			'DrawText2(e.Graphics, Group.Name, Font, Color.DarkRed, r1, StrFormat("lm"))
			DrawText2(e.Graphics, Group.Name, New Font(Font.Name, Font.Size, FontStyle.Bold), Color.Black, r1, StrFormat("lm"))
		Next
		For i = 1 To Columns - 1
			Dim x = (ClientRectangle.Width \ Columns) * i
			'e.Graphics.DrawLine(SystemPens.ButtonShadow, x, FieldPadding * 2, x, Height - FieldPadding * 2)
		Next
		For Each Field In IntFields
			Field.Draw(e.Graphics, Font)
		Next
	End Sub

	Protected Overrides Sub OnSizeChanged(e As System.EventArgs)
		MyBase.OnSizeChanged(e)
		Rearrange()
	End Sub

	Private Sub CreateLabels()
		AddLabel("sno", "Sigorta no.")
		AddLabel("kimlik", "Kimlik no.")
		AddLabel("adi", "Adı")
		AddLabel("soyadi", "Soyadı")
		AddLabel("dosyano", "Dosya no.")
		AddLabel("fisno", "Fiş no.")
		AddLabel("isyerino", "İşyeri no.")
		AddLabel("isyeriadi", "İşyeri adı")
		AddLabel("islemtar", "İşlem tar.")
		AddLabel("recetetar", "Reçete tar.")
		AddLabel("murtar", "Müracaat tar.")
		AddLabel("kaykul", "Kullanıcı")
		AddLabel("hturu", "Hesap türü")
		AddLabel("kararno", "Karar no.")
		AddLabel("sdrm", "Aktif/Pasif")
		AddLabel("sube", "Şube no.")
		AddLabel("kod", "Kayıt no.")
		AddLabel("miktar", "Tutar")
		AddLabel("tutar", "Tutar")
		AddLabel("adres", "Adres")
		AddLabel("kime", "Alacaklı")
		AddLabel("ceknoyeni", "Çek no.")
		AddLabel("cekkod", "Çek türü")
		AddLabel("cekno", "Referans no.")
		AddLabel("rowid", "Kayıt kodu")
		AddLabel("odeemrino", "Ödeme e.no.")
		AddLabel("ay", "Ay")
		AddLabel("ayeki", "Ay eki")
		AddLabel("donem", "Dönem")
		AddLabel("yil", "Yıl")
		AddLabel("iptalkod", "İptal kodu")
		AddLabel("boztar", "Boz./ipt.tar.")
		AddLabel("onay", "Onay")
		AddLabel("tarih", "Tarih")
		AddLabel("bhesnotar", "Hesap d.tarihi")
		AddLabel("fatura", "Fatura no.")
		AddLabel("adr", "Adres")
		AddLabel("adrsemt", "Semt")
		AddLabel("adrsehir", "Şehir")
		AddLabel("tel", "Telefon")
		AddLabel("email", "E-posta")
		AddLabel("pasaport", "Pasaport no.")
		AddLabel("dtar", "Doğum tar.")
		AddLabel("dogumtar", "Doğum tar.")
		AddLabel("dsehir", "Doğ. şehir")
		AddLabel("dulke", "Doğ. ülke")
		AddLabel("baba", "Baba adı")
		AddLabel("anne", "Anne adı")
		AddLabel("cins", "Cinsiyet")
		AddLabel("medeni", "Medeni dur.")
		AddLabel("uyr", "Uyruk")
		AddLabel("meslek", "Meslek")
		AddLabel("egitim", "Eğitim")
		AddLabel("odeyerkod", "Ödeme yeri")
		AddLabel("odeyerkodbanka", "Banka")
		AddLabel("odeyerkodsube", "Şube")
		AddLabel("bankahno", "Banka hes.no")
		'Geçindirmekle yükümlü
		AddLabel("gyskimlik", "Kimlik no.")
		AddLabel("gysadi", "Adı")
		AddLabel("gyssoyadi", "Soyadı")
		'Eczane
		AddLabel("eczkod", "Eczane")
		AddLabel("eczsahibi", "Eczane sahibi")
		AddLabel("eczsozbittar", "Sözleşme bit.tar.")
		AddLabel("muaynetar", "Muayene tar.")
		AddLabel("faturano", "Fatura no.")
		AddLabel("receteno", "Reçete no.")
		AddLabel("ydurum", "Yakınlığı")
		'Gözlük
		AddLabel("cerodememe", "Ödememe nedeni")
		AddLabel("camodememe", "Ödememe nedeni")
		'Yaşlılık
		AddLabel("bolgekod", "Bölge")
		AddLabel("demeklisi", "Devlet emk.")
		AddLabel("calisdrm", "Çalışıyor mu?")
		AddLabel("ymbastar", "Maaş b.tarihi")
		AddLabel("derece", "Derece")
		AddLabel("basamak", "Basamak")
		AddLabel("yderece", "Yeni derece")
		AddLabel("ybasamak", "Yeni basamak")
		AddLabel("katsayi1", "Katsayı 1")
		AddLabel("katsayi2", "Katsayı 2")
		AddLabel("ykatsayi1", "Yeni katsayı 1")
		AddLabel("kcpogs", "Kıbrıs Cumhuriyeti")
		AddLabel("ssdpogs", "Sosyal Sigortalar")
		AddLabel("tcpogs", "Türkiye Cumhuriyeti")
		AddLabel("muchpogs", "Mücahitlik")
		AddLabel("tmtpogs", "TMT")
		AddLabel("devhpogs", "Devlet hizmetleri")
		AddLabel("borcpogs", "Borçlanılan")
		AddLabel("borc95Apogs", "Borçlanılan (95)")
		AddLabel("ortalamapogs", "Ortalama PÖGS")
		AddLabel("toplampogs", "Toplam PÖGS")
		AddLabel("taleppogs", "Son 5 yıl")
	End Sub

	Protected Overrides Sub OnLeave(e As System.EventArgs)
		Selected = Nothing
		Pressed = Nothing
		MyBase.OnLeave(e)
	End Sub

	Sub AddGroup(Name As String, Fields As String)
		Dim Group As New Group
		Group.Name = Name
		SuspendLayout()
		For Each Item In Fields.Split(";")
			AddField(Item, Group)
		Next
		ResumeLayout()
		Groups.Add(Group)
		Rearrange()
	End Sub

	Protected Overrides Sub OnMouseLeave(e As System.EventArgs)
		MyBase.OnMouseLeave(e)
		Selected = Nothing
	End Sub

	Protected Overrides Sub OnMouseMove(e As System.Windows.Forms.MouseEventArgs)
		MyBase.OnMouseMove(e)
		For Each Field In IntFields
			If Field.Rect.Contains(e.Location) And Not Field.Locked And Field.Enabled Then
				Selected = Field
				Return
			End If
		Next
		Selected = Nothing
	End Sub

	Protected Overrides Sub OnMouseDown(e As System.Windows.Forms.MouseEventArgs)
		MyBase.OnMouseDown(e)
		If Not Selected Is Nothing Then Pressed = Selected
	End Sub

	Class Group
		Public Rect As Rectangle
		Public Name As String
		Public Fields As New List(Of Field)
	End Class

	Class Field

		Public DataWindow As DataWindow
		Public Group As Group
		Public Display As String
		Public Rect As Rectangle
		Public Name As String
		Public Type As String
		Public WithEvents Control As TextBoxBase

		Dim MinWidth As Integer
		Dim ExtraWidth As Integer

		Sub New(dw As DataWindow, Name As String, Type As String)
			DataWindow = dw
			Me.Name = Name
			Me.Type = Type
			Select Case Type
				'Case "odeyerkod" : AddCombo(Muhasebe.OdemeYerleriSource) : MinWidth = 300
				Case "odeyerkodbanka" : AddCombo(Muhasebe.OdemeYerleriBankaSource)
				Case "odeyerkodsube" : AddCombo(Muhasebe.OdemeYerleriSubeSource)
				Case "onay", "demeklisi", "calisdrm" : AddCombo("E,EVET|H,HAYIR")
				Case "sdrm" : AddCombo("A,AKTİF|P,PASİF")
				Case "cins" : AddCombo("E,ERKEK|K,KADIN")
				Case "ydurum" : AddCombo("K,KENDİSİ|E,EŞİ|Ç,ÇOCUĞU")
				Case "medeni" : AddCombo("E,EVLİ|B,BEKAR|D,DUL")
				Case "uyr", "dulke" : AddCombo(SysKod.Get("01.03"))
				Case "cerodememe" : AddCombo(SysKod.Get("05.02"))
				Case "camodememe" : AddCombo(SysKod.Get("05.03"))
				Case "egitim" : AddCombo(SysKod.Get("01.02"))
				Case "eczkod" : AddCombo(Saglik.Eczane.EczaneSource)
				Case "bolgekod" : AddCombo("1,LEFKOŞA|2,GİRNE|3,MAĞUSA|4,İSKELE|5,GÜZELYURT|6,LEFKE|7,MERKEZ")
				Case "tutar", "miktar" : AddMoney()
				Case "recetetar", "muaynetar", "dtar", "dogumtar", "bhesnotar", "ymbastar"
					Control = New DateBox
					DataWindow.Controls.Add(Control)
				Case Else
					Control = New TextBox
					CType(Control, TextBox).Casing = CharacterCasing.Upper
					DataWindow.Controls.Add(Control)
			End Select
			Control.Name = Name
			Select Case Type
				Case "receteno"
					Control.MaxLength = 7
					Control.AllowOnlyNumeric = True
				Case "faturano"
					Control.MaxLength = 7
					Control.AllowOnlyNumeric = True
			End Select
			Control.Reset()
		End Sub

		ReadOnly Property IsModified As Boolean
			Get
				If DataWindow.Recordset Is Nothing Then Return False
				If Not DataWindow.Recordset.Columns.Contains(Name) Then Return False
				If DataWindow.Recordset.EOF Then Return Not Control.IsEmpty
				Return Not Trim(IsNull(DataWindow.Recordset(Name), "")) = Trim(IsNull(Control.Value, ""))
			End Get
		End Property

		Dim _Locked As Boolean
		Property Locked As Boolean
			Get
				Return Not _Locked
			End Get
			Set(value As Boolean)
				_Locked = Not value
				Control.Enabled = Not Locked AndAlso Enabled
				Control.BackColor = Color.White
				Invalidate()
			End Set
		End Property

		Dim _Enabled As Boolean = True
		Property Enabled As Boolean
			Get
				Return _Enabled
			End Get
			Set(value As Boolean)
				_Enabled = value
				Control.Enabled = Not Locked AndAlso Enabled
				Control.BackColor = Color.White
				Invalidate()
			End Set
		End Property

		ReadOnly Property Selected As Boolean
			Get
				Return Me Is DataWindow.Selected
			End Get
		End Property

		ReadOnly Property Pressed As Boolean
			Get
				Return Me Is DataWindow.Pressed
			End Get
		End Property

		Dim r1, r2, r3 As Rectangle

		Sub Draw(g As Graphics, Font As Font)
			Dim R = Rect
			Dim r1 = Me.r1
			Dim r2 = Me.r2
			Dim r3 = Me.r3
			g.SmoothingMode = Drawing2D.SmoothingMode.None
			g.PixelOffsetMode = Drawing2D.PixelOffsetMode.None
			'Dim Pen As New Pen(Themes.Current.FormColor)
			Dim Pen As New Pen(Color.Black.Opacity(128))
			Pen.DashStyle = Drawing2D.DashStyle.Dot
			'g.DrawLine(Color.Black.Opacity(16).Pen, R.Left, R.Top, R.Right - 1, R.Top)
			'g.DrawLine(Color.Black.Opacity(16).Pen, r1.Right - 2, r1.Top, r1.Right - 2, r1.Bottom - 1)
			g.DrawLine(Pen, R.Left, R.Bottom - 1, R.Right - 1, R.Bottom - 1)
			g.DrawLine(Pen, r1.Right - 2, r1.Top + 2, r1.Right - 2, r1.Bottom - 2)
			If Pressed Then
				R = r2
				R.Offset(0, 1)
				R.Inflate(0, -1)
				R.Height -= 1
				R.Width -= 1
				g.FillRectangle(Brushes.White, R)
			End If
			If Pressed Then
				R.Width += ExtraWidth
				r2.Width += ExtraWidth
				r3.Offset(ExtraWidth, 0)
			End If
			Dim LabelColor As Color = If(Not Enabled, Themes.Current.LabelColor.Mix(Color.White, 30), If(Pressed Or Selected, Color.Black, Themes.Current.LabelColor))
			Dim ValueColor As Color = If(Not Locked, Color.Black, Themes.Current.LabelColor)
			r1.Inflate(-4, 0)
			DrawText2(g, Display, Font, LabelColor, r1, StrFormat("lm"))
			Dim w = TextRenderer.MeasureText(Display, Font).Width
			r1.Width -= w
			r1.Offset(w, 0)
			Dim o = 0
			r2.Inflate(-1, -2)
			r2.Offset(-1, 0)
			g.PixelOffsetMode = Drawing2D.PixelOffsetMode.HighQuality
			Dim a = 192
			If Locked Then a = 0
			If Not Enabled Then a = 96
			If Pressed Or Selected Then a = 255

			g.SmoothingMode = Drawing2D.SmoothingMode.None
			If Not Pressed And Selected Then
				DrawImage(g, My.Resources.editing, r3, 0.5)
				o += r3.Width
			ElseIf Pressed Then
				R = r2
				R.Inflate(1, -1)
				R.Width -= 3
				g.PixelOffsetMode = Drawing2D.PixelOffsetMode.None
				DrawImage(g, My.Resources.editing, r3)
				o += r3.Width
			ElseIf IsModified Then
				r3.Inflate(-3, -3)
				DrawImage(g, My.Resources.modified, r3)
				r3.Inflate(3, 3)
				o += r3.Width
			End If
			If Not Pressed And Enabled Then
				R = r2
				R.Inflate(-3, 0)
				R.Width -= o
				DrawText2(g, Trim(IsNull(Control.Text, "")), Font, ValueColor, R, StrFormat("lm"))
			End If
		End Sub

		Sub Invalidate()
			Dim R = Rect
			R.Width += ExtraWidth
			'DataWindow.Invalidate(R)
			DataWindow.Invalidate()
		End Sub

		Sub Focus()
			DataWindow.Focused = Me
		End Sub

		Sub Locate(R As Rectangle)
			Rect = R
			Dim x As Integer = R.Width * DataWindow.Rate
			r1 = New Rectangle(R.Left, R.Top, x + 1, R.Height)
			r2 = New Rectangle(R.Left + x + 1, R.Top, R.Width - (x + 1), R.Height)
			Dim dr = r2
			dr.Width -= 3
			dr.Offset(1, 3)
			If Not TypeOf Control Is ComboBox Then dr.Width -= r2.Height
			ExtraWidth = If(dr.Width < MinWidth, MinWidth - dr.Width, 0)
			dr.Width += ExtraWidth
			Control.Size = dr.Size
			If Not Control Is Nothing Then
				Control.Location = If(Pressed, dr.Location, New Point(-10000, 0))
			End If
			r3 = r2
			r3.Offset(r2.Width - r3.Height, 0)
			r3.Width = r3.Height
			r3.Inflate(-4, -4)
		End Sub

		Private Sub Control_GotFocus(sender As Object, e As System.EventArgs) Handles Control.GotFocus
			Dim r2 = Me.r2
			DataWindow.Pressed = Me
			Application.DoEvents()
		End Sub

		Sub ShowHide(Show As Boolean)
			Dim r2 = Me.r2
			r2.Offset(1, 3)
			Control.Location = If(Show, r2.Location, New Point(-10000, 0))
		End Sub

		Private Sub Control_LostFocus(sender As Object, e As System.EventArgs) Handles Control.LostFocus
			DataWindow.Tip.Destroy()
		End Sub

		Private Sub AddCombo(Source As String)
			Control = New ComboBox
			DataWindow.Controls.Add(Control)
			CType(Control, ComboBox).Source = Source
			CType(Control, ComboBox).DataWindow = True
		End Sub

		Private Sub AddMoney()
			Control = New MoneyBox
			DataWindow.Controls.Add(Control)
		End Sub

		ReadOnly Property IsEmpty As Boolean
			Get
				Return Control.IsEmpty
			End Get
		End Property

		ReadOnly Property IsDirty As Boolean
			Get
				Return Control.IsDirty
			End Get
		End Property

		Sub Reset()
			Control.Reset()
		End Sub

		Property Value As Object
			Get
				Return Control.Value
			End Get
			Set(value As Object)
				Control.Value = value
			End Set
		End Property

	End Class

	Dim Tip As New BalloonTip

	Public Function Check(ByVal Control As String, ByVal Condition As Boolean, Optional ByVal Message As String = "") As Boolean
		If Not Condition Then Return False
		If Message <> "" Then
			If Message.StartsWith("!") Then
				Warning(Mid(Message, 2))
			Else
				Try
					Pressed = Fields(Control)
					Fields(Control).Focus()
					Fields(Control).Control.SelectAll()
				Catch ex As Exception
				End Try
				Tip.Style = BalloonTip.TipStyle.Balloon
				Tip.Icon = BalloonTip.IconStyle.Warning
				Tip.Title = "Uyarı"
				Tip.TipText = Message
				Tip.PopupOnDemand = True
				Dim Handle As Int32 = Controls(Control).Handle.ToInt32
				Tip.CreateToolTip(Handle)
				Tip.Show(32, Controls(Control).Height + 2, Handle)
			End If
		End If
		Return True
	End Function

	Sub Save()
		For Each Item In IntFields
			If Recordset.Columns.Contains(Item.Name) Then
				If Not Item.Locked Then Recordset(Item.Name) = Item.Control.Value
			End If
		Next
	End Sub

	Private Sub FieldGenericEvent(Sender As Object, EventType As GenericEventType, Param As String)
		Static NoEvent As Boolean = False
		Select Case EventType
			Case GenericEventType.Change
				Select Case Sender.name
					Case "odeyerkod"
						If Not NoEvent Then
							NoEvent = True
							If Trim(Sender.value) <> "" Then
								Dim B = Muhasebe.OdemeYerleriBanka(Process.Remote, Sender.Value)
								If B <> "" Then Fields("odeyerkodbanka").Value = B
							End If
							Fields("odeyerkodsube").Value = Sender.Value
							NoEvent = False
						End If
					Case "odeyerkodbanka"
						Dim sube As ComboBox = Fields("odeyerkodsube").Control
						sube.Reset()
						sube.Source = Muhasebe.OdemeYerleriSubeSource(Sender.Value)
					Case "odeyerkodsube"
						If Not NoEvent Then
							NoEvent = True
							Fields("odeyerkod").Value = Sender.Value
							NoEvent = False
						End If
				End Select
				SortedFields(Sender.Name).First.Invalidate()
				RaiseActionEvent(Sender.Name & ".change")
		End Select
	End Sub

	Private Sub FieldLeave(sender As Object, e As EventArgs)
		SortedFields(sender.Name).First.Invalidate()
		RaiseActionEvent(sender.Name & ".leave")
	End Sub

	Private Sub FieldValidatingHandler(sender As Object, e As System.ComponentModel.CancelEventArgs)
		RaiseEvent FieldValidating(sender, e)
	End Sub

	Function ContainsField(Name As String) As Boolean
		Return SortedFields.Contains(Name)
	End Function

End Class
